<?php

namespace app\forms\user;

use yii\db\Exception;
use app\forms\Model;
use app\models\User;
use yii\web\HttpException;
use app\validators\PositiveInteger;

/**
 * 用户 首页
 */
class IndexForm extends Model
{
    public $groupId;
    public $deptId;
    public $realName;

    public $page;
    public $pageSize;

    public function rules()
    {
        return [
            ['groupId', PositiveInteger::class],

            ['deptId', PositiveInteger::class],

            ['realName', 'trim'],
            ['realName', 'string', 'max' => 16],

            ['page', PositiveInteger::class],
            ['page', 'default', 'value' => 1],

            ['pageSize', PositiveInteger::class],
            ['pageSize', 'default', 'value' => 10],
        ];
    }

    /**
     * @param $param
     * @return array[]
     * @throws HttpException
     */
    public static function index($param)
    {
        $form = new self();
        $form->setAttributes($param);
        if (!$form->validate()) {
            foreach ($form->getErrors() as $key => $val)
                $form->$key = '';
        }
        $attr = $form->getAttributes();

        try {
            return (new User)->getUserList($attr);
        } catch (Exception $e) {
            $form->serverError();
        }

    }
}